package Forms;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JSeparator;

import Logic.Registration;

public class RegistrationFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtFieldFirstName;
	private JTextField txtFieldLastName;
	private JTextField txtFieldAddress;
	private JTextField txtFieldNationality;
	private JPasswordField pwdField;
	private JPasswordField pwdFieldRepeat;
	private JSpinner spinnerBirthdate;
	private Date birthDate;
	private DateFormat dateFormat;
	private MainForm mainFrame;


	

	/**
	 * Create the frame.
	 * registration form for new passengers/users
	 */
	public RegistrationFrame(MainForm _mainFrame) {
		setTitle("Registration Form");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 425, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		mainFrame = _mainFrame;

		JPanel panelMain = new JPanel();
		contentPane.add(panelMain, BorderLayout.CENTER);
		panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));

		JPanel panelName = new JPanel();
		panelMain.add(panelName);
		FlowLayout fl_panel = new FlowLayout(FlowLayout.LEADING, 5, 5);
		panelName.setLayout(fl_panel);

		JLabel lblNewLabel = new JLabel("First Name:");
		panelName.add(lblNewLabel);

		txtFieldFirstName = new JTextField();
		panelName.add(txtFieldFirstName);
		txtFieldFirstName.setColumns(10);

		JLabel lblLastName = new JLabel("Last Name:");
		panelName.add(lblLastName);

		txtFieldLastName = new JTextField();
		panelName.add(txtFieldLastName);
		txtFieldLastName.setColumns(10);

		JSeparator separator = new JSeparator();
		panelMain.add(separator);

		JPanel panelAddress = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panelAddress.getLayout();
		flowLayout.setAlignment(FlowLayout.LEADING);
		panelMain.add(panelAddress);

		JLabel lblAddress = new JLabel("Address:");
		panelAddress.add(lblAddress);

		txtFieldAddress = new JTextField();
		panelAddress.add(txtFieldAddress);
		txtFieldAddress.setColumns(30);

		JSeparator separator_1 = new JSeparator();
		panelMain.add(separator_1);

		JPanel panelNationality = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panelNationality.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEADING);
		panelMain.add(panelNationality);

		JLabel lblNationality = new JLabel("Nationality(IT,DE,CH..):");
		panelNationality.add(lblNationality);

		txtFieldNationality = new JTextField();
		txtFieldNationality.setHorizontalAlignment(SwingConstants.CENTER);
		panelNationality.add(txtFieldNationality);
		txtFieldNationality.setColumns(3);

		JPanel panelSpacer = new JPanel();
		panelNationality.add(panelSpacer);

		JLabel lblBirthDate = new JLabel("Birth-date:");
		panelNationality.add(lblBirthDate);
		spinnerBirthdate = new JSpinner();
		panelNationality.add(spinnerBirthdate);
		spinnerBirthdate.setModel(new SpinnerDateModel(
				new Date(1360926000000L), new Date(-2202652800000L), new Date(
						1363302000000L), Calendar.YEAR));
		spinnerBirthdate.setEditor(new JSpinner.DateEditor(spinnerBirthdate,
				"dd-MM-yyyy"));
		spinnerBirthdate.setPreferredSize(new Dimension(90, 27));

		JSeparator separator_2 = new JSeparator();
		panelMain.add(separator_2);
		SpinnerDateModel model = new SpinnerDateModel();
		model.setCalendarField(Calendar.YEAR);

		JPanel panelPwd = new JPanel();
		panelMain.add(panelPwd);
		panelPwd.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

		JLabel lblPassword = new JLabel("Password (6 - 20):");
		lblPassword.setHorizontalAlignment(SwingConstants.LEFT);
		panelPwd.add(lblPassword);

		pwdField = new JPasswordField();
		pwdField.setHorizontalAlignment(SwingConstants.LEFT);
		pwdField.setColumns(20);
		pwdField.setText("");
		panelPwd.add(pwdField);

		JPanel panelRepeatPwd = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panelRepeatPwd.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEADING);
		panelMain.add(panelRepeatPwd);

		JLabel lblRepeat = new JLabel("Repeat Password:");
		panelRepeatPwd.add(lblRepeat);

		pwdFieldRepeat = new JPasswordField();
		pwdFieldRepeat.setHorizontalAlignment(SwingConstants.LEFT);
		pwdFieldRepeat.setColumns(20);
		panelRepeatPwd.add(pwdFieldRepeat);

		JSeparator separator_5 = new JSeparator();
		panelMain.add(separator_5);

		JPanel panelButtons = new JPanel();
		panelMain.add(panelButtons);

		JButton btnCancel = new JButton("Cancel");
		panelButtons.add(btnCancel);
		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});

		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if (checkFields()) {
					Registration registration = new Registration(
							txtFieldFirstName.getText().trim(),
							txtFieldLastName.getText().trim(), txtFieldAddress
									.getText().trim(), txtFieldNationality
									.getText().trim(),
							dateFormat.format((Date) spinnerBirthdate
									.getValue()), new String(pwdField
									.getPassword()).trim());
					String registrationString = mainFrame
							.registerUser(registration);
					JOptionPane.showMessageDialog(getContentPane(),
							registrationString);
					if (registrationString
							.contains("Registration Successfull!")) {

						dispose();
					}
				}

			}
		});
		panelButtons.add(btnRegister);

		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		setVisible(true);

	}
	//checks if fields are filled correctly

	public boolean checkFields() {
		if (txtFieldFirstName.getText().trim().equals("")) {
			JOptionPane.showMessageDialog(this, "First name missing!");
			return false;
		}
		if (txtFieldLastName.getText().trim().equals("")) {
			JOptionPane.showMessageDialog(this, "Last name missing!");
			return false;
		}
		if (txtFieldAddress.getText().trim().equals("")) {
			JOptionPane.showMessageDialog(this, "Address missing!");
			return false;
		}
		if (txtFieldNationality.getText().trim().equals("")) {
			JOptionPane.showMessageDialog(this, "Nationality missing!");
			return false;
		}
		try {

			birthDate = dateFormat.parse(dateFormat
					.format((Date) spinnerBirthdate.getValue()));
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
			return false;
		}

		String pwdMain = new String(pwdField.getPassword()).trim();
		if (pwdMain.length() < 6) {
			JOptionPane.showMessageDialog(this, "Password to short!");
			return false;
		}
		if (pwdMain.length() > 20) {
			JOptionPane.showMessageDialog(this, "Password to long!");
			return false;
		}
		if (!pwdMain.equals(new String(pwdFieldRepeat.getPassword()).trim())) {
			JOptionPane.showMessageDialog(this, "Passwords don't match!");
			return false;
		}
		return true;

	}

}
